/**
 * 通用uni-app网络请求
 * 基于 Promise 对象实现更简单的 request 使用方式，支持请求和响应拦截
 */

/*
// 开放的接口

*/
import util from './util';
import {useAppStore} from '@/stores/app';
import {apiUrl, apiNotoken, tenantId} from './config';
function Client (method:string, action:string, params:object, callback:any, failback:any, opt?:object , type?:string) {
    const appStore = useAppStore();
    let default_options = {
        url: apiUrl + action,
        header:{
            'TENANT-ID': tenantId,
            'CLIENT-TOC': 'Y'
        },
        data: params,
        method: method.toUpperCase(),
    };
    let options = opt || {};
    if(options.hasOwnProperty('header')){
        options.header = Object.assign({},default_options.header, options.header);
    }
    let accessToken= uni.getStorageSync('accessToken');
    if(apiNotoken.indexOf(action) == -1){
        if(accessToken){
            default_options.header['Authorization'] = 'Bearer ' + accessToken;
        }
    }
    // 如果参数中有formData的属性，就用formData的方式传参
    if(params.hasOwnProperty('formData')){
        default_options.header['content-Type'] = 'application/x-www-form-urlencoded';
        default_options.data = util.json2Form(params.formData);
    }
    if(method == 'get'){
        default_options.header['content-Type'] = 'application/x-www-form-urlencoded';
    }
    if(type == 'upload'){
        default_options.header['content-Type'] = 'multipart/form-data';
    }
    // 请求
    uni.showLoading();
    return new Promise((resolve, reject) => {
        let complete = {
            complete: (res) => {
                uni.hideLoading();
                if(res.statusCode == 200){ // 请求成功
                    uni.hideLoading();
                    if(type == 'all'){
                        let {code,data,msg} = res.data;
                        if(res.data.hasOwnProperty('code')){
                            if(code == 401 && !appStore.showMsgBox){ 
                                appStore.showMsgBox = true;
                                util.gotoLogin('未登录或登录过期，请前去登录');
                            }
                        }else{                  
                            resolve(res.data);
                            if(typeof(callback) === 'function'){
                                callback(res.data);
                            }
                        }
                        return;
                     }
                    if(res.errMsg == 'request:fail ' || res.data.status == 503){
                        if (typeof (failback) === 'function') {
                            failback(res);
                        }
                        return false;
                    }
                    let {code,data,msg} = res.data;                    
                    if (code == 0 || code == 200) {
                        resolve(data);
                        if(typeof(callback) === 'function'){
                            callback(data, msg);
                        }
                    } else {
                        if(code == 401 && !appStore.showMsgBox){ 
                            appStore.showMsgBox = true;
                            util.gotoLogin('未登录或登录过期，请前去登录');
                        }else{
                            uni.showToast({title: msg, icon: 'none'});
                            reject(data);
                            if (typeof (failback) === 'function') {
                                failback(msg);
                            }
                        }
                        // if(type == 'all'){
                        //     reject(res.data);
                        //     if (typeof (failback) === 'function') {
                        //         failback(res.data);
                        //     }
                        // }else{
                        //     reject(data);
                        //     if (typeof (failback) === 'function') {
                        //         failback(msg);
                        //     }
                        // }
                    }
                }else if(res.statusCode == 401 || res.statusCode == 424){
                    if((res.data.msg == '没有登录' || res.data.msg == '用户凭证已过期') && !appStore.showMsgBox){
                        appStore.showMsgBox = true;
                        util.gotoLogin('未登录或登录过期，请前去登录');
                    }
                }else{ // 请求失败
                    if (typeof (failback) === 'function') {
                        failback(res.statusCode);
                    }
                }
            }            
        };        
        let new_options = Object.assign({}, default_options, complete, options);
        uni.request(new_options);
    })
}

export default Client;
